<template>
  <div class="app-container">
    <!-- 操作查询面板 -->
    <div class="inquire-top">
      <!-- 控制 -->
      <div class="filter-container">
        <el-button
          class="filter-item"
          type="primary"
          @click="addBlick()"
        >新增</el-button>
        <el-button
          class="filter-item"
          type="primary"
          @click="addBlick()"
        >导出</el-button>
        <el-button
          class="filter-item"
          type="primary"
          @click="resetBlick()"
        >重置</el-button>
        <el-button
          class="filter-item"
          type="primary"
          @click="handleSearch()"
        >查询
        </el-button>
        <el-button
          style="margin-top: 10px; margin-left: 10px; margin-bottom: 0px"
          type="text"
          class="filter-item text_button"
          @click="inquireBotton()"
        ><i class="el-icon-circle-plus-outline" />操作</el-button>
      </div>
      <div v-show="isshow" class="search-input">
        <el-form ref="form" :model="searchFrom" label-width="110px">
          <el-row :gutter="24">
            <el-col :span="4">
              <el-form-item label="司机姓名:">
                <el-input
                  v-model="searchFrom.name"
                  placeholder="司机姓名"
                  class="filter-item"
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="司机电话:">
                <el-input
                  v-model="searchFrom.mobile"
                  placeholder="司机电话"
                  class="filter-item"
                />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="司机状态:">
                <el-select
                  v-model="searchFrom.driverStatus"
                  multiple
                  filterable
                  placeholder="状态"
                >
                  <el-option label="认证中" value="PENDING" />
                  <el-option label="已认证" value="SUCCESS" />
                  <el-option label="认证失败" value="FALL" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="车辆状态:">
                <el-select
                  v-model="searchFrom.carStatus"
                  multiple
                  filterable
                  placeholder="状态"
                >
                  <el-option label="认证中" value="PENDING" />
                  <el-option label="已认证" value="SUCCESS" />
                  <el-option label="认证失败" value="FALL" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="操作时间:">
                <el-date-picker
                  v-model="applyForTime"
                  type="datetimerange"
                  range-separator=" ~ "
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['00:00:00', '23:59:59']"
                  placeholder="选择时间范围"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
    <!-- 车辆状态  标签页-->
    <div class="vehicle-state">
      <el-table
        v-loading="listLoading"
        :data="list"
        border
        stripe
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" align="center" width="50" />

        <el-table-column label="姓名" min-width="110px" align="center">
          <template slot-scope="{ row }">
            <span class="link-type" @click="handleDetail(row)">
              {{ row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.mobile }}</span>
          </template>
        </el-table-column>
        <el-table-column label="身份证号码" min-width="150px" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.identityCardId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车牌号" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.licenseNo }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车型车长" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.typeName+' '+row.lengthName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车辆所有人" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.owner }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否自有车" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.isSelfCar==='true'?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作人" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.lastUpdateUserName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.lastUpdateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="车辆状态" align="center">
          <template slot-scope="{ row }">
            <span>{{ status[row.carStatus] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="司机状态" align="center">
          <template slot-scope="{ row }">
            <span>{{ status[row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" align="center">
          <template slot-scope="{ row }">
            <span>{{ row.createdTime }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          width="230"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              v-permission="['SYSTEM_USER','TENANT_USER']"
              type="danger"
              size="mini"
              @click="handleUpdate(row)"
            >审核</el-button>

            <el-button
              v-if="row.status != 'deleted'"
              size="mini"
              type="primary"
              @click="handleDetail(row)"
            >详情</el-button>
            <el-button
              v-if="row.status != 'deleted'"
              size="mini"
              type="primary"
              @click="delDriverAndCar(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.current"
        :limit.sync="listQuery.size"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { getDriverAndCarList, delDriverAndCar } from '@/api/tms/driver'
export default {
  name: 'Index',
  components: { Pagination },
  data() {
    return {
      applyForTime: '',
      searchFrom: {
        name: '',
        mobile: '',
        startTime: '',
        endTime: '',
        driverStatus: null,
        carStatus: null
      },
      status: {
        'PENDING': '认证中',
        'SUCCESS': '已认证',
        'FALL': '认证失败'
      },
      list: [],
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20
      },
      total: 0,
      isshow: false
    }
  },
  watch: {
    applyForTime(newValue) {
      this.searchFrom.startTime = newValue[0]
      this.searchFrom.endTime = newValue[1]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 新增司机
    addBlick() {
      this.$router.push({ path: '/resourceManager/driver/add' })
    },
    // 重置
    resetBlick() {
      this.searchFrom = {
        name: '',
        mobile: '',
        startTime: '',
        endTime: '',
        driverStatus: null,
        carStatus: null
      }
      this.applyForTime = []
      this.getList()
    },
    // 点击 审核
    handleUpdate(row) {
      this.$router.push({ path: `/userInfoManage/driver/audit/${row.id}` })
    },
    // 点击 详情
    handleDetail(row) {
      this.$router.push({ path: `/resourceManager/driver/detail/${row.id}` })
    },
    delDriverAndCar(row) {
      this.$confirm('此操作将删除该司机, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          sourceId: row.userId,
          bizId: row.carId
        }
        delDriverAndCar(data).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList() {
      const data = Object.assign({}, this.listQuery, this.searchFrom)
      console.log(data)
      getDriverAndCarList(data).then((response) => {
        console.log('车辆列表', response.records)
        this.list = response.records
        this.total = response.total
        this.listLoading = false
      })
    },
    inquireBotton() {
      this.isshow = !this.isshow
    },
    handleSearch() {
      this.listQuery.current = 1
      this.getList()
    }
  }
}
</script>
<style lang="scss">
.filter-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-right: 10px;
  .align-items {
    margin-bottom: 0;
  }
  .filter-item {
    margin-top: 10px;
    margin-left: 10px;
    margin-bottom: 0px;
  }
}

.text_button {
  font-size: 16px;
}

.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: center;

  button {
    width: 100px;
  }
}

.search-input {
  width: 100%;
  display: flex;
  align-items: center;
}
</style>
